package com.fyt.ZhongYaoApp.mapper;

import com.fyt.ZhongYaoApp.pojo.Collection;
import com.fyt.ZhongYaoApp.pojo.Essay;
import com.fyt.ZhongYaoApp.pojo.vo.EssayCollectionVO;
import com.fyt.ZhongYaoApp.pojo.vo.ZYCollectionVO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface CollectionMapper {

    @Insert("insert into collection(user_id,essay_id,zy_id,create_time)" +
            "values (#{userId},#{essayId},#{zyId},now())")
    void addCollection(Collection collection);

    /**
     * 根据用户id查询他收藏的文章
     * @param userId
     * @return
     */
    @Select("select " +
            "`e`.`id`," +
            "`c`.`essay_id`," +
            "`e`.`title`," +
            "`e`.`author`," +
            "`e`.`content`," +
            "`c`.`create_time`" +
            "from `essay` AS e " +
            "LEFT JOIN `collection` AS c ON `c`.`essay_id` = `e`.`id`" +
            "WHERE `c`.`user_id` = #{userId}")
    List<EssayCollectionVO> essayList(Integer userId);

    /**
     * 根据用户id查询他收藏的中药
     * @param userId
     * @return
     */
    @Select("select `z`.`id`, `c`.`zy_id`, `z`.`name`,z.zy_pic, `z`.`amount`" +
            " from `zy` AS z" +
            " LEFT JOIN `collection` AS c ON `z`.`id` = `c`.`zy_id`" +
            " WHERE `user_id` = #{userId}")
    List<ZYCollectionVO> zyList(Integer userId);


//    @Select("select * from collection where user_id = #{userId} and essay_id = #{essayId}")
    Collection findEssayif(Collection collection);


    void cancelCollection(Collection collection);
}
